/*
 * ImageDecoderEx.h
 *
 *  Created on: 2 janv. 2011
 *      Author: Serge
 */

#ifndef WORKERTHREAD_H_
#define WORKERTHREAD_H_

#include <e32base.h>
#include <e32msgqueue.h>
#include "Function.h"
#include <euserhl.h>





struct CLocalThreadCallback;

class CWorkerThread : public CBase
	{
private:
	static TInt ThreadFunction(TAny* aPtr);
	RThread iWorkerThread;
	RMsgQueue< scalp::CFunction<void()>* > iMsgQueue;
	RMsgQueue< scalp::CFunction<void()>* > iLocalMsgQueue; // used for deleting only
	CLocalThreadCallback* iLocalThreadCallback;
	scalp::CFunction<void()>* iInitFunction;
	TRequestStatus iThreadInitStatus;
	TThreadId iMainThreadId;
	TBool iTerminateThread;
	void TerminateThread();
	
	
public:
	CWorkerThread();
	virtual ~CWorkerThread();
	void Execute( scalp::CFunction<void()>* aFunction );
	void Start(scalp::CFunction<void()>* aFunction = NULL);
	void Stop();
	void Rendezvous(TRequestStatus& aStatus);
	
	
	};


struct MAsyncRequestCallback
	{
	virtual void RemoteCallback();
	virtual void LocalCallback(TInt Error);
	virtual void RemoteCleanup();
	virtual ~MAsyncRequestCallback();
	};

struct CAsyncRequest_Local;

struct CAsyncRequest
	{
	CAsyncRequest_Local* iLocal;
	CAsyncRequest();
	void CallL(CWorkerThread* aWorkerThread, MAsyncRequestCallback* aCallback);
	void Cancel();
	~CAsyncRequest();
	};


#endif /* WORKERTHREAD_H_ */
